package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.bean.InfantindexBean;
import com.meihua.baoleyi.bean.Relatives;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.pickerviewlibrary.SymptomsPickerView;

import java.util.List;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by jzm on 2017/11/03
 * 个人资料
 */

public class SelectSymptomsActivity extends BaseActivity {
    Relatives relative;
    int position;
    //年龄
    @Bind(R.id.more)
    ImageView more;
    @Bind(R.id.tvRelative)
    TextView tvRelative;
    @Bind(R.id.layoutRelatives)
    RelativeLayout layoutRelatives;
    //生病
    @Bind(R.id.iv_more_three)
    ImageView mIv_more;
    @Bind(R.id.tv_day)
    TextView mTv_day;
    @Bind(R.id.rl_illcycle)
    RelativeLayout mRl_illcycle;
    //性别
    @Bind(R.id.submit)
    TextView mTv_submit;
    @Bind(R.id.rl_sex)
    RelativeLayout mRl_sex;
    @Bind(R.id.tv_sex)
    TextView mTv_sex;
    private String currentAgePosition = "1";
    private String currentSexPosition = "1";
    private String currentCyclePosition = "1";
    private boolean isNetOk = false; //数据成功的标识

    private static final int TYPE_AGE = 1; //年龄
    private static final int TYPE_SEX = 2; //性别
    private static final int TYPE_CYCLE = 3; //周期

    private SymptomsPickerView mAgePickerView; //年龄选择器
    private SymptomsPickerView mSexPickerView; //性别选择器
    private SymptomsPickerView mCyclePickerView; //周期选择器

    public static void start(Context context, Relatives relatives, int position) {
        Intent intent = new Intent(context, SelectSymptomsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("relatives", relatives);
        intent.putExtra("position", position);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_symptoms);
        ButterKnife.bind(this);
        getServerData();
    }
    /*获取服务器数据*/
    private void getServerData() {
        showDialog();
        TreeMap<String, String> treeMap = new TreeMap<String, String>();
        NetHelper.post(APIServer.INFANT_INDEX, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                /*请求得到的数据*/
                InfantindexBean bean = NetHelper.getBeanForJson(InfantindexBean.class, s);
                if (bean == null) return;
                if (bean.getCode().equals("100")){
                    isNetOk = true; //设置标识为成功
                    //年龄选择器
                    agepicker(bean.getData().getAge());
                    //性别选择器
                    sexpicker(bean.getData().getSex());
                    //生病周期选择器
                    sickpicker(bean.getData().getCycle());
                    //设置每个选择器默认数据
                    tvRelative.setText(bean.getData().getAge().get(0));
                    mTv_sex.setText(bean.getData().getSex().get(0));
                    mTv_day.setText(bean.getData().getCycle().get(0));
                } else {
                    ToastUtil.show(SelectSymptomsActivity.this, bean.getMsg());
                }
                dismissDialog();
            }
        });
    }

    //性别选择器
    private void sexpicker(List<String> sexList) {
        mSexPickerView = new SymptomsPickerView(this, sexList, TYPE_SEX);
        mSexPickerView.setTextSize(15f);
        mSexPickerView.setmOnRelativeMoreListener(new SymptomsPickerView.OnRelativeMoreListener() {
            @Override
            public void onRelaviteSelectMore(String ageStr, int position) {
                mTv_sex.setText(ageStr);
                currentSexPosition = position + "";
            }
        });
    }

    //年龄选择器
    private void agepicker(List<String> ageList) {
        mAgePickerView = new SymptomsPickerView(this, ageList, TYPE_AGE);
        mAgePickerView.setTextSize(15f);
        mAgePickerView.setmOnRelativeMoreListener(new SymptomsPickerView.OnRelativeMoreListener() {
            @Override
            public void onRelaviteSelectMore(String ageStr, int position) {
                tvRelative.setText(ageStr); //设置年龄提示
                currentAgePosition = position + "";
            }
        });
    }

    //生病周期选择器
    private void sickpicker(List<String> cycleList) {
        mCyclePickerView = new SymptomsPickerView(this, cycleList, TYPE_CYCLE);
        mCyclePickerView.setTextSize(15f);
        mCyclePickerView.setmOnRelativeMoreListener(new SymptomsPickerView.OnRelativeMoreListener() {
            @Override
            public void onRelaviteSelectMore(String cycleStr, int position) {
                mTv_day.setText(cycleStr);
                currentCyclePosition = position +"";
            }
        });
    }

    /**
     * 点击事件
     * @param view
     */
    @OnClick({R.id.iv_back, R.id.layoutRelatives, R.id.rl_illcycle, R.id.submit, R.id.rl_sex})
    public void onViewClicked(View view) {
        hiddenInputMethod();
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.layoutRelatives://年龄
                if (isNetOk) {
                    mAgePickerView.show();
                }
                break;
            case R.id.rl_sex:  //性别
                if (isNetOk) {
                    mSexPickerView.show();
                }
                break;
            case R.id.rl_illcycle://生病
                if (isNetOk) {
                    mCyclePickerView.show();
                }
                break;
            case R.id.submit://下一步
                submitPost();
                break;
        }
    }

    /**
     * 点击下一步
     */
    private void submitPost() {
        if (isNetOk) {
            Intent intent = new Intent(SelectSymptomsActivity.this, SelectDiseaseActivity.class);
            intent.putExtra("age",currentAgePosition);
            intent.putExtra("sex",currentSexPosition);
            intent.putExtra("cycle",currentCyclePosition);
            startActivity(intent);
        } else {
            ToastUtil.show(SelectSymptomsActivity.this, "服务暂时有问题");
        }
    }

}

